//
//  HomeViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/5.
//  Copyright © 2017年 RCMTM. All rights reserved.
//
//判断系统版本大于10.0
#define IOS_VERSION_10 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_x_Max)?(YES):(NO)
#import "HomeViewController.h"
#import "HomeChildModel.h"
#import "LoginViewController.h"
#import "ClassViewController.h"
#import "ClassPageViewController.h"
#import "HomeDetailViewController.h"
#import "SearchViewController.h"
#import "SearchClassShowView.h"

#import "ClassForCollectionViewCell.h"
#import "ClassCollectionReusableView.h"

#import "ADView.h"
#import "ADModel.h"

#import "ClassModel.h"
#import "HomeButtonView.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "MaterielDetailViewController.h"

#define itemWidth (SCREEN_W-60)/5.0
#define itemHeight itemWidth/(100/140.0)+55
static const CGFloat ADVIEW_TAG = 100000;
static const CGFloat C_Button_Tag = 300000;
static const CGFloat ClassButton_Tag = 200000;
static const CGFloat ButtonView_Tag = 210000;
@interface HomeViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic) UICollectionView * collectionView;
@property (strong, nonatomic) NSMutableArray * dataArray;
@property (strong, nonatomic) NSMutableArray * buttonArray;
@property (strong, nonatomic) NSMutableArray * buttonViewArray;
@property (strong, nonatomic) NSMutableArray * chooseViewArray;
//@property (weak, nonatomic) IBOutlet UIView *searchView;

//顶部View
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;
- (IBAction)searchButton:(id)sender;

//轮播图View
@property (strong, nonatomic) ADView *adView;
//按钮View
@property (weak, nonatomic) IBOutlet UIView *buttonView;
@property (weak, nonatomic) IBOutlet UIView *chooseView;
@property (weak, nonatomic) IBOutlet UIView *c_lineView;

@property (strong, nonatomic) SearchClassShowView * s_classView;
@property (strong, nonatomic) UIView * b_backView;
@property (strong, nonatomic) NSArray *AdArray;
@property (assign, nonatomic) BOOL isSearch;
@property (strong, nonatomic) NSString * searchStr;
@property (assign, nonatomic) BOOL isWuliao;//判断是否是物料

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    if (NO)// [UserManager key].length <= 0)
    {
        self.tabBarController.tabBar.hidden = YES;
        [self hiddenUI];
        LoginViewController * loginVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"LoginViewController"];
        loginVC.isHome = YES;
        [self presentViewController:loginVC animated:NO completion:nil];
    }else{
        self.tabBarController.tabBar.hidden = NO;
        [self loadUIForCollectionView];
        [self loadUI];
        
    }
   
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.public_headView.hidden = YES;
    [LoginViewController blockForLogin:^(BOOL hasLogin) {
        self.tabBarController.tabBar.hidden = NO;
        [self loadUIForCollectionView];
        [self loadUI];
       
    }];
    [ClassCollectionReusableView blockForHeadTouch:^(ClassModel *model) {
        if (model.indexPath.section == 0) {
            return ;
        }
        ClassPageViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"ClassPageViewController"];
        tmpVC.hidesBottomBarWhenPushed  = YES;
        tmpVC.index = (int)model.indexPath.section;
        [self.navigationController pushViewController:tmpVC animated:YES];
    }];
    
    [SearchClassShowView blockForSearchClass:^(int index) {
     
        ClassPageViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"ClassPageViewController"];
        tmpVC.hidesBottomBarWhenPushed  = YES;
        tmpVC.index = index;
        [self.navigationController pushViewController:tmpVC animated:YES];
    }];
    [SearchClassShowView blockForSearchButton:^(NSString *searchStr,BOOL isreturn) {
      
        SearchViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"SearchViewController"];
        NSArray * classArr = [NSArray arrayWithObjects:@"衣服",@"男士衬衫",@"男士西装",@"西裤",@"扣子",@"布料",@"风衣",@"毛呢", nil];
        NSMutableArray * needArr = [[NSMutableArray alloc]init];
        for (int i=0; i<classArr.count; i++) {
            ClassModel * model = [[ClassModel alloc]init];
            model.name = [classArr objectAtIndex:i];
            model.class_id = [NSString stringWithFormat:@"%d",i];
            [needArr addObject:model];
        }
        tmpVC.dataArray = [NSMutableArray arrayWithArray:needArr];
        tmpVC.hidesBottomBarWhenPushed  = YES;
        tmpVC.keyWord = searchStr;
        [self.navigationController pushViewController:tmpVC animated:YES];
        
    }];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_adView startAnimation:NO];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 懒加载

- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
        NSArray * nameArr = [NSArray arrayWithObjects:@"新款展示",@"特色服装",@"女士西服",@"女士风衣",@"女士衬衫",nil];
        NSArray * contentArr = [NSArray arrayWithObjects:@"引领潮流新风尚",@"引领潮流新风尚",@"引领潮流新风尚",@"引领潮流新风尚",@"引领潮流新风尚",nil];
        NSArray * imageArr = [NSArray arrayWithObjects:@"",@"",@"nv_c1",@"nv_c2",@"nv_c3",nil];
        NSArray * typeArr = [NSArray arrayWithObjects:@(CollectionHeaderTypeForLunbo),@(CollectionHeaderTypeForFont),@(CollectionHeaderTypeForPhoto),@(CollectionHeaderTypeForPhoto),@(CollectionHeaderTypeForPhoto),nil];
        for (int i=0 ; i<nameArr.count; i++) {
            ClassModel * model = [[ClassModel alloc]init];
            model.name = [nameArr objectAtIndex:i];
            model.content = [contentArr objectAtIndex:i];
            model.imageUrl = [imageArr objectAtIndex:i];
            model.headType = [[typeArr objectAtIndex:i] intValue];
            model.childArray  =[NSMutableArray array];
            for (int j = 0; j<5; j++) {
                ClassModel * tmp_Model = [[ClassModel alloc]init];
                tmp_Model.name = @"详细内容";
                tmp_Model.imageUrl = @"nv1";
                [model.childArray addObject:tmp_Model];
            }
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}
- (NSMutableArray *)buttonArray;
{
    if (!_buttonArray) {
        _buttonArray = [[NSMutableArray array]init];
        NSArray * nameArr = [NSArray arrayWithObjects:@"经典男装",@"极致女装",@"物料其他", nil];
        NSArray * valueArr = [NSArray arrayWithObjects:@"Classic Menswear",@"Ultimate Dress",@"Other Materiel", nil];
        NSArray * imageArr = [NSArray arrayWithObjects:@"nan",@"nv",@"wuliao", nil];
        for (int i=0 ; i<3; i++) {
            ClassModel * model = [[ClassModel alloc]init];
            model.name = [nameArr objectAtIndex:i];
            model.value = [valueArr objectAtIndex:i];
            model.imageUrl = [imageArr objectAtIndex:i];
            [_buttonArray addObject:model];
        }
    }
    return _buttonArray;
}
- (NSArray *)AdArray;
{
    if (!_AdArray) {
        _AdArray = [[NSArray alloc]init];
        
    }
    return _AdArray;
}
- (NSMutableArray *)buttonViewArray {
    
    if (!_buttonViewArray) {
        
        _buttonViewArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < self.buttonArray.count; i ++) {
            HomeButtonView *view = [[HomeButtonView alloc] init];
            [_buttonView addSubview:view];
            [_buttonViewArray addObject:view];
        }
    }
    
    return _buttonViewArray;
}

- (NSMutableArray *)chooseViewArray {
    
    if (!_chooseViewArray) {
        NSArray * nameArr = @[@"西服料册",@"衬衫料册",@"辅料展示",@"量体工具"];
        _chooseViewArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < nameArr.count; i ++) {
            UIButton *view = [[UIButton alloc] init];
            [_chooseView addSubview:view];
            [_chooseViewArray addObject:view];
        }
    }
    
    return _chooseViewArray;
}

#pragma mark -  UI布局
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    CGFloat searchViewH = 124;
    CGFloat adView_H = SCREEN_W/(2048./700.);
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 20;
    CGFloat buttonW =(SCREEN_W -4*jianGeW)/3.0;
    CGFloat buttonH = buttonW/5.0*3;
    CGFloat s_imageW = 40;
    CGFloat s_imageH = 40;
    [self loadADView];
    _searchButton.frame = CGRectMake(SCREEN_W-s_imageW*2, 10, s_imageW, s_imageH);
    _s_classView.frame = CGRectMake(0, 0, SCREEN_W, searchViewH);
    _buttonView.frame = CGRectMake(0, _adView.frame.origin.y+_adView.frame.size.height+jianGeH, SCREEN_W, buttonH);
    [self loadButtonView];
    if (_chooseView.hidden) {
        _chooseView.frame = CGRectMake(0, _buttonView.frame.origin.y+_buttonView.frame.size.height+15, SCREEN_W, 0);
    }else{
        _chooseView.frame = CGRectMake(0, _buttonView.frame.origin.y+_buttonView.frame.size.height+15, SCREEN_W, 40);
    }
    [self loadChooseView];
    _headerView.frame = CGRectMake(0, 0, SCREEN_W, _chooseView.frame.origin.y+_chooseView.frame.size.height+15);
    [_collectionView setContentOffset:CGPointZero];
    [_collectionView reloadData];
//    [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
//    [_collectionView reloadData];
    
}
-(void)hiddenUI
{
    _buttonView.hidden = YES;
    _headerView.hidden = YES;
}

-(void)loadUIForCollectionView
{
    
    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置具体属性
    // 1.设置 最小行间距
    layout.minimumLineSpacing = 10;
    // 2.设置 最小列间距
    layout. minimumInteritemSpacing  = 10;
    // 3.设置item块的大小 (可以用于自适应)
    layout.estimatedItemSize = CGSizeMake(itemWidth, itemHeight);
    // 设置滑动的方向 (默认是竖着滑动的)
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    // 设置item的内边距
    layout.sectionInset = UIEdgeInsetsMake(1,10,1,10);
    //设置collectionView头视图的大小
    [layout setHeaderReferenceSize:CGSizeMake(_collectionView.frame.size.width,20)];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_W, SCREEN_H-USERDEFINED_TABBAR_H) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        NSString *version = [UIDevice currentDevice].systemVersion;
        if (version.doubleValue >= 11) { // iOS系统版本 >= 11 .0
            make.top.equalTo(@-20);
        }else{
            make.top.equalTo(@0);
        }
        make.bottom.equalTo(self.view);
    }];
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ClassForCollectionViewCell class] forCellWithReuseIdentifier:@"ClassForCollectionViewCell"];
    [self.collectionView registerClass:[ClassCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderTypeForLunbo"];
    [self.collectionView registerClass:[ClassCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderTypeForFont"];
    [self.collectionView registerClass:[ClassCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderTypeForPhoto"];
    [_collectionView reloadData];
}


-(void)loadUI
{
    _buttonView.hidden = NO;
    _headerView.hidden = NO;
    CGFloat adView_H = SCREEN_W/(2048./700.);
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 20;
    CGFloat buttonW =(SCREEN_W -4*jianGeW)/3.0;
    CGFloat buttonH = buttonW/5.0*3;
    CGFloat headView_H = adView_H+buttonH+jianGeH*2;
    NSArray * Arr = [NSArray arrayWithObjects:@"衣服",@"男士衬衫",@"男士西装",@"西裤",@"扣子",@"布料",@"风衣",@"毛呢",nil];
    NSMutableArray * classArr = [[NSMutableArray alloc]init];
    for (int i=0; i<Arr.count; i++) {
        ClassModel * model = [[ClassModel alloc]init];
        model.name = [Arr objectAtIndex:i];
        model.class_id = [NSString stringWithFormat:@"%d",i];
        [classArr addObject:model];
    }
    CGFloat searchViewH = 124;
    _s_classView = [[SearchClassShowView alloc]init];
    [self.view addSubview:_s_classView];
//    [self.view bringSubviewToFront:_s_classView];
    _s_classView.frame = CGRectMake(0, 0, SCREEN_W, searchViewH);

    [_s_classView loadSearchViewWithClassArray:classArr andHasBack:NO andFirstChoose:@"-1"];
    _s_classView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    _s_classView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    _s_classView.layer.shadowOpacity = 0.8;//阴影透明度，默认0
    _s_classView.layer.shadowRadius = 4;//阴影半径，默认3
    _s_classView.hidden = YES;

    _headerView.frame = CGRectMake(0, 0, SCREEN_W, headView_H);
    CGFloat s_imageW = 40;
    CGFloat s_imageH = 40;
    _adView = [[ADView alloc]init];
    [self.headerView addSubview:_adView];
     [self loadADView];
    _searchButton.frame = CGRectMake(SCREEN_W-s_imageW*2, 10, s_imageW, s_imageH);
    _searchButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, -10, -10);
    [_headerView bringSubviewToFront:_searchButton];
    _buttonView.frame = CGRectMake(0, adView_H+jianGeH, SCREEN_W, buttonH);
//    _buttonView.backgroundColor = [UIColor greenColor];
    _b_backView = [[UIView alloc]init];
    [_buttonView addSubview:_b_backView];
    [self loadButtonView];
    buttonW = SCREEN_W/4.0;
    _chooseView.frame = CGRectMake(0, _buttonView.frame.origin.y+_buttonView.frame.size.height+15, SCREEN_W, 0);
    _chooseView.hidden = YES;
    _c_lineView.backgroundColor = QianZi_color;
    [_chooseView addSubview:_c_lineView];
    [self.chooseView addLineOnPosition:UIViewLinePositionBottom color:Line_color lineWidth:3 insets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self loadChooseView];
}

-(void)loadChooseView
{
    CGFloat lineViewH = 3;
    NSArray * nameArr = @[@"西服料册",@"衬衫料册",@"辅料展示",@"量体工具"];
    CGFloat viewW = SCREEN_W/nameArr.count;
    CGFloat viewH = 25;
    for (int i=0; i<nameArr.count; i++) {
        UIButton * button = [self.chooseViewArray objectAtIndex:i];
        button.frame = CGRectMake(viewW*i, (40-viewH)/2.0, viewW, viewH);
        [button addTarget:self action:@selector(chooseButtonFunc:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = Cell_Big_Font;
        [button setTitleColor:QianZi_color forState:UIControlStateNormal];
        button.tag = C_Button_Tag+i;
        [button setTitle:nameArr[i] forState:UIControlStateNormal];
    }
    UIButton * tmpButton = [_chooseView viewWithTag:C_Button_Tag];
    [tmpButton setTitleColor:ShenZi_Color forState:UIControlStateNormal];
    _c_lineView.frame = CGRectMake(tmpButton.frame.origin.x, 40-lineViewH, tmpButton.frame.size.width, lineViewH);
    
    [_chooseView bringSubviewToFront:_c_lineView];
}
-(void)loadButtonView
{
    
    CGFloat jianGeW = 25;
    CGFloat buttonW =(SCREEN_W -4*jianGeW)/3.0;
    CGFloat buttonH = buttonW/5.0*3;
    HomeButtonView * l_view = nil;
    for (int i=0; i<self.buttonViewArray.count; i++) {
        ClassModel * model = [_buttonArray objectAtIndex:i];
        HomeButtonView * view = [_buttonViewArray objectAtIndex:i];
//        [_buttonView addSubview:view];
//        view.frame = CGRectMake(jianGeW+(buttonW+jianGeW)*i,0, buttonW, buttonH);
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (l_view!=nil) {
                make.left.equalTo(l_view.mas_right).offset(jianGeW);
                make.width.equalTo(@(buttonW));
                make.height.equalTo(@(buttonH));
                make.top.equalTo(_buttonView);
            }else{
                make.left.equalTo(_buttonView).offset(jianGeW);
                make.width.equalTo(@(buttonW));
                make.height.equalTo(@(buttonH));
                make.top.equalTo(_buttonView);
            }
        }];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonFunc:)];
        view.tag = ButtonView_Tag+i;
        [view addGestureRecognizer:tap];
        //给View加圆角和阴影
//        CALayer *layer = [CALayer layer];
//        layer.frame = CGRectMake(0, 0, buttonW, buttonH);
//        layer.backgroundColor = [UIColor whiteColor].CGColor;
//        layer.shadowOffset = CGSizeMake(0, 0);
//        layer.shadowColor =RGBColor(205, 205, 205).CGColor;
//        layer.shadowOpacity = 0.4;
//        layer.cornerRadius = 8.0;
//        [layer removeFromSuperlayer];
//        [view.layer addSublayer:layer];
        [view loadHomeButtonWithModel:model];
        
        l_view = view;
    }
  
    UIView * tmpView = [_buttonView viewWithTag:ButtonView_Tag];
    [_b_backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(tmpView);
    }];
//    _b_backView.frame = CGRectMake(tmpView.frame.origin.x, tmpView.frame.origin.y, buttonW, buttonH);
    _b_backView.backgroundColor = RGBColorAlpha(0, 0, 0, 0.4);
    [_buttonView bringSubviewToFront:_b_backView];
    [_b_backView setYuanJiaoForRadius:8.0 andMasks:NO];
}
-(void)loadADView//设置广告位
{
    CGFloat adView_H = SCREEN_W/(2048./700.);
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    NSArray * imageArr = [NSArray arrayWithObjects:@"chang2",@"chang2",@"chang2", nil];
    for (int i=0; i<imageArr.count; i++) {
        ADModel* model = [[ADModel alloc]init];
        model.cover = [imageArr objectAtIndex:i];
        //adView内部的默认图片改了，记得改回来。
        [arr addObject:model];
    }
    self.AdArray = arr;
  
    _adView.frame = CGRectMake(0, 0,SCREEN_W ,adView_H);
    [_adView loadADViewWithNumber:[self.AdArray count] WithImageFrame:CGSizeMake(SCREEN_W, adView_H) WithHorizontal:YES WithAnimationTime:2.0];
//    _adView=[[ADView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_W ,adView_H) WithNumber:[self.AdArray count] WithImageFrame:CGSizeMake(SCREEN_W, adView_H) WithHorizontal:YES WithAnimationTime:2.0];
    _adView.tag = ADVIEW_TAG;
    NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[self.AdArray count]; i++) {
        ADModel* model = [self.AdArray objectAtIndex:i];
        NSString *productADString  = model.cover;
        [tmpImageArray addObject:productADString];
    }
//    _adView.imageColor = [UIColor redColor];
    if ([self.AdArray count]>0) {
        [_adView initNOFitImageViewWithImagesURL:tmpImageArray andPlaceholderImageName:DEFAULT_HOMEAD_PHOTO_RECTANGLE];
    }
    
    [_adView clickOneImage:^(NSInteger index)
     {
         
         //点击图片，然后
         if (index < [self.AdArray count])
         {
             
             ADModel* model = [self.AdArray objectAtIndex:index];
             if (GlobalHandleValue(model.jump) == nil || [GlobalHandleValue(model.jump) isEqualToString:@""]) {
                 return ;
             }
             
         }
         
         
     }];
    
    [_adView addPageControl];
    [_adView startAnimation:YES];
    
}
// 完成拖拽(滚动停止时调用此方法，手指离开屏幕前)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat velocity = scrollView.contentOffset.y;
    CGFloat searchViewH = 124;
    if (velocity >0) {
        _s_classView.hidden = NO;
        _isSearch = YES;
        //向下拖动，显示导航栏
        
        [UIView animateWithDuration:0.37 animations:^{
            
            _s_classView.frame = CGRectMake(0, 0, SCREEN_W, searchViewH);
        }];
        
    }else if (velocity <=0) {
        _isSearch = NO;
        //向上拖动，隐藏导航栏
        [UIView animateWithDuration:0.37 animations:^{
            _s_classView.frame = CGRectMake(0, -searchViewH, SCREEN_W, searchViewH);
        } completion:^(BOOL finished) {
            _s_classView.hidden = YES;
        }];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
//    //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
//    CGFloat velocity = [pan velocityInView:scrollView].y;
    CGFloat velocity = scrollView.contentOffset.y;
    CGFloat searchViewH = 124;
    if (velocity >0) {
        _s_classView.hidden = NO;
        _isSearch = YES;
        //向下拖动，显示导航栏
        
        [UIView animateWithDuration:0.37 animations:^{
            
            _s_classView.frame = CGRectMake(0, 0, SCREEN_W, searchViewH);
        }];
        
    }else if (velocity <=0) {
        _isSearch = NO;
        //向上拖动，隐藏导航栏
        [UIView animateWithDuration:0.37 animations:^{
            _s_classView.frame = CGRectMake(0, -searchViewH, SCREEN_W, searchViewH);
        } completion:^(BOOL finished) {
           _s_classView.hidden = YES;
        }];
    }
    
}

#pragma mark - 手势按钮方法

-(void)chooseButtonFunc:(UIButton*)button
{
    CGFloat lineViewH = 3;
    for (int i=0; i<4; i++) {
        UIButton *tmpButton = [_chooseView viewWithTag:C_Button_Tag+i];
        [tmpButton setTitleColor:QianZi_color forState:UIControlStateNormal];
        
    }
    [button setTitleColor:ShenZi_Color forState:UIControlStateNormal];
    [UIView animateWithDuration:0.37 animations:^{
        _c_lineView.frame = CGRectMake(button.frame.origin.x, 40-lineViewH, button.frame.size.width, lineViewH);
    }];
    
    
}
-(void)buttonFunc:(UITapGestureRecognizer*)tap
{
    if (tap.view.tag-ButtonView_Tag == 2) {
        _isWuliao = YES;
        _chooseView.frame = CGRectMake(0, _buttonView.frame.origin.y+_buttonView.frame.size.height+15, SCREEN_W, 40);
        _headerView.frame = CGRectMake(0, 0, SCREEN_W, _chooseView.frame.origin.y+_chooseView.frame.size.height+15);
        _chooseView.hidden = NO;
    }else{
        _isWuliao = NO;
        _chooseView.frame = CGRectMake(0, _buttonView.frame.origin.y+_buttonView.frame.size.height+15, SCREEN_W, 0);
        _headerView.frame = CGRectMake(0, 0, SCREEN_W, _chooseView.frame.origin.y+_chooseView.frame.size.height+15);
        _chooseView.hidden = YES;
    }
    [_collectionView reloadData];
    [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
    [_collectionView reloadData];
    UIView * tmpView = tap.view;
    _b_backView.frame = CGRectMake(tmpView.frame.origin.x, tmpView.frame.origin.y, tmpView.frame.size.width, tmpView.frame.size.height);
}
- (IBAction)searchButton:(id)sender {
    _isSearch = !_isSearch;
    CGFloat searchViewH = 124;
    if (_isSearch){
        _s_classView.hidden = NO;
        //向下拖动，显示导航栏
        [UIView animateWithDuration:0.37 animations:^{
           
            _s_classView.frame = CGRectMake(0, 0, SCREEN_W, searchViewH);
        }];
        
    }else{
        //向上拖动，隐藏导航栏
        [UIView animateWithDuration:0.37 animations:^{
            _s_classView.frame = CGRectMake(0, -searchViewH, SCREEN_W, searchViewH);
        } completion:^(BOOL finished) {
            _s_classView.hidden = YES;
        }];
        
    }
    
   
}

#pragma mark --- UICollectionViewDelegate

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.dataArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWidth, itemHeight);
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ClassModel *model = [_dataArray objectAtIndex:indexPath.section];
    NSString * ID = @"";
    switch (model.headType) {
        case CollectionHeaderTypeForFont:
        {
            ID = @"CollectionHeaderTypeForFont";
        }
            break;
        case CollectionHeaderTypeForPhoto:
        {
            ID = @"CollectionHeaderTypeForPhoto";
        }
            break;
        case CollectionHeaderTypeForLunbo:
        {
            ID = @"CollectionHeaderTypeForLunbo";
        }
            break;
        default:
            break;
    }
    model.indexPath = indexPath;
    ClassCollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ID forIndexPath:indexPath];
    CGFloat imageW = SCREEN_W;
    CGFloat imageH = SCREEN_W/(2048./600.);
//    headerView.frame = CGRectMake(0, imageH*indexPath.section, imageW, imageH);
    headerView.Cell_W = imageW;
    headerView.headType = model.headType;
    switch (model.headType) {
        case CollectionHeaderTypeForFont:
        {
            headerView.Cell_H = 60;
            headerView.offsetY = 0;
        }
            break;
        case CollectionHeaderTypeForPhoto:
        {
            headerView.Cell_H = imageH;
        }
            break;
        case CollectionHeaderTypeForLunbo:
        {
            headerView.Cell_H = 60;
            headerView.offsetY = _headerView.frame.origin.y+_headerView.frame.size.height;
            [headerView addSubview:_headerView];
        }
            break;
        default:
            break;
    }
    [headerView loadHeaderWithModel:model];
    return headerView;
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    ClassModel *model = [_dataArray objectAtIndex:section];
    CGFloat height = 0;
    switch (model.headType) {
        case CollectionHeaderTypeForFont:
        {
            height = 60;
        }
            break;
        case CollectionHeaderTypeForPhoto:
        {
            height = SCREEN_W/(2048./600.);
        }
            break;
        case CollectionHeaderTypeForLunbo:
        {
            height = 60+_headerView.frame.origin.y+_headerView.frame.size.height;
        }
            break;
        default:
            break;
    }
    CGSize size = {SCREEN_W, height};
    return size;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    ClassModel * model = [self.dataArray objectAtIndex:section];
    return model.childArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 需要配置的代码
    //    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    ClassForCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassForCollectionViewCell" forIndexPath:indexPath];
    ClassModel * tmpModel = [_dataArray objectAtIndex:indexPath.section];
    ClassModel * model = [tmpModel.childArray objectAtIndex:indexPath.row];
    
    [cell setCellWithModel:model];
    
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_isWuliao) {
        MaterielDetailViewController * tmpVC = [[MaterielDetailViewController alloc]init];
        tmpVC.title = @"物料详情";
        tmpVC.hidesBottomBarWhenPushed  = YES;
        [self.navigationController pushViewController:tmpVC animated:YES];
    }else{
        HomeDetailViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"HomeDetailViewController"];
        tmpVC.hidesBottomBarWhenPushed  = YES;
        tmpVC.title = @"详情页";
        [self.navigationController pushViewController:tmpVC animated:YES];
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
